﻿using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Web.UI;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;

public partial class Admin_UserManage_HurmPermissionList : System.Web.UI.Page
{
    public CompetenceLib CL = new CompetenceLib();
    Dcms_PermissionBLL pBll = new Dcms_PermissionBLL();
    Dcms_PermissionModel pModel = new Dcms_PermissionModel();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {          
            CL.IsLogin();

            if (!IsPostBack)
            {
                BindData();
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Admin_UserManage_PermissionList.Page_Load", ex);
            throw ex;
        }
    }

    private void BindData()
    {
        this.repAdmin.DataSource = pBll.GetModelList();
        this.repAdmin.DataBind();
    }

    protected void repAdmin_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            //隔行变色
            if (e.Item.ItemIndex % 2 == 1)
            {
                ((HtmlTableRow)e.Item.FindControl("tr1")).Style.Add("background", "#E8F0FB");
            }

            ((LinkButton)e.Item.FindControl("HypDelete")).Attributes.Add
                        ("onclick", "return confirm('删除功能模块同时也会删除改模块下的所有类别,确定要删除该记录吗?');");
               ((HyperLink)e.Item.FindControl("hypEdit")).NavigateUrl = String.Format
                            ("EditPermission.aspx?ID=" + ((HiddenField)e.Item.FindControl("hidID")).Value + "&PermissionID=" + strPermissionID);
        }
    }

    protected void repAdmin_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (e.CommandName == "delete")
            {
                if (CL.IsDelete("PERMISSION"))
                {
                    Dcms_PermissionModel pModel =  pBll.GetModelByID(Convert.ToInt32(((HiddenField)e.Item.FindControl("hidID")).Value));
                    pBll.DeleteModelByID(Convert.ToInt32(((HiddenField)e.Item.FindControl("hidID")).Value));
                    //删除分类
                    Dcms_CateBLL cBll = new Dcms_CateBLL();
                    cBll.DeleteModelByFlag(pModel.Permission_Flag, pModel.Permission_ModelKeyId);
                    //删除模块字段管理
                    Dcms_ModelFieldBLL mBll = new Dcms_ModelFieldBLL();
                    mBll.DeleteModelByModelAndModelKeyID(pModel.Permission_Flag, pModel.Permission_ModelKeyId);
                    Session.Add("Message", "删除功能模块及类别成功，请返回！");
                    Session.Add("Url", "Modle/ModelField/Permission/PermissionList.aspx?id=" + strPermissionID);
                    Response.Redirect("../Message.aspx",false);
                }
            }
       }
    }

    protected string strPermissionID
    {
        get
        {
            return Request.QueryString["id"] + "";
        }
    }
}
